import http from "http"
import url from "url"
import path from "path"
import fs from "fs"
let obj = {
    css: "text:css;charset=utf-8",
    txt: "text:txt;charset=utf-8",
    js: "text:js;charset=utf-8",
    html: "text:html;charset=utf-8",


}
const server = http.createServer((req,res) => {

    let ourl =url.parse(req.url)
    
    let {
        pathname
    } = ourl
    let urls = path.join(__dirname, pathname)
    let last = urls.slice(urls.lastIndexOf(".") + 1)
    if (fs.existsSync(urls)) {
        if (fs.statSync(urls).isFile()) {
            let str = fs.readFileSync(urls)
            if (obj[last]) {
                res.writeHead(200, "ok", {
                    "content-type": obj[last]
                })
                res.write(str)
                res.end()
                return;
            }
        }
      
    }
      res.end("404")
})

server.listen(3000, () => {})